package com.dongke.controller;

import com.dongke.Text.MD5Utils;
import com.dongke.pojo.Role1;
import com.dongke.pojo.User1;
import com.dongke.service.RoleService;
import com.dongke.service.User_1Service;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("User_1Controller")
public class User_1Controller {

    @Autowired
    private User_1Service uUser_1Service;
    @Autowired
    private RoleService roleService;

    @RequestMapping("toShow")
    public String toShow(){

        return "user/show";
    }

    @RequestMapping("getInfo")
    @ResponseBody
    public Map<Object,Object> getInfo(
            User1 user
            , @RequestParam(required = false, defaultValue = "5") Integer pageSize
            , @RequestParam(required = false, defaultValue = "1") Integer pageNum
    ){
        HashMap<Object, Object> map = new HashMap<>();
        PageInfo<User1> page = uUser_1Service.selectInfo(user, pageSize, pageNum);
        map.put("total", page.getTotal());//总条数
        map.put("rows", page.getList());//结果集
        return map;
    }

    @RequestMapping("toAdd")
    public String toAdd(Model model){
       List<Role1> list =  roleService.selectRole();
        model.addAttribute("list",list);
        return "user/toAdd";
    }
    @RequestMapping("addUserInfo")
    @ResponseBody
    public boolean addUserInfo(User1 user, @RequestParam("rid") Integer[] rids) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        if (user.getUname() != null || !"".equals(user.getUname())){
            User1 u = uUser_1Service.getUserInfo(user);
            if (u == null){
                String upassword = MD5Utils.getEncryptedPwd("123");
                user.setPwd(upassword);
                return uUser_1Service.addUserInfo(user, rids);
            }
            return false;
        }
        return false;
    }

    //获取当前角色信息跳转至修改页面
    @RequestMapping("updateToRole")
    public String updateToRole(Integer uid, Model model){
        User1 user = uUser_1Service.getInfo(uid);
        List<Role1> list = uUser_1Service.getRoleInfo();
        List<Integer> ids = uUser_1Service.getUserRoleById(uid);
        model.addAttribute("user", user);
        model.addAttribute("list", list);
        model.addAttribute("ids", ids);
        return "user/updateToRole";
    }
    //将修改后的数据填充至数据库
    @RequestMapping("updateUserById")
    @ResponseBody
    public Boolean updateUserById(User1 user, @RequestParam("rid") Integer[] rid){
        return uUser_1Service.updateUserById(user, rid);
    }
    //删除单个账户
    @RequestMapping("deleteInfoById")
    @ResponseBody
    public boolean deleteInfoById(Integer uid){
        return uUser_1Service.deleteInfoById(uid);
    }
}
